/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Patient;
import healthcare.coursework.model.Service;
import healthcare.coursework.service.AppointmentService;
import healthcare.coursework.service.UserService;
import healthcare.coursework.util.Utility;
import java.util.Date;
import java.util.List;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Timebox;
import org.zkoss.zul.Window;
import org.zkoss.zul.Messagebox;

/**
 *
 * @author Administrator
 */
public class BookAppointment extends GenericAutowireComposer {

    private AppointmentService appointmentService = util.Utility.getUtility().getAppointmentService();
    private UserService userService = util.Utility.getUtility().getUserService();
    private Combobox service;
    private Datebox day;
    private Timebox time;
    private Combobox doctorName;
    private Window self;
    private Patient patient;
    private Label patientName;


    private void setInfo() {
        List<Service> services=new ListModelList(appointmentService.listOfServices());
        
        service.setModel(new ListModelList(services));
        doctorName.setModel(new ListModelList(util.Utility.filterRole(userService.findUsers(), "Doctor")));
        patient = (Patient) arg.get("patient");
        patientName.setValue(patient.getFullName());
        day.setValue(new Date());
    }
    public void onLoadForm(Event e){
        setInfo();
        setConstraint();
    }

    private void setConstraint() {
        day.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateAppointTime(cmpnt, o);
            }
        });
    }

    private void validateAppointTime(Component comp, Object value) throws WrongValueException {
        Date d = (Date) value;
        if (d.before(new Date())) {
            throw new WrongValueException(comp, "The day of appointment must be greater than or equal with current date!!!");
        }
    }

    public void onCancel(Event e) {
        self.onClose();
        Executions.sendRedirect("/adminStaff/ViewAppointments.do?patientId=" + patient.getPatientId());
    }

    public void onBook(Event e) {
        try {

            Date appointmentTime=util.Utility.convertToDate(day.getValue(), time.getValue());
            appointmentService.bookDiagnoseAppointment(patient.getPatientId(),
                    (Integer) service.getSelectedItem().getValue(), (String) doctorName.getSelectedItem().getValue(),
                    appointmentTime);
            Messagebox.show("Appointment at time :"+
                    appointmentTime.toString()+ " with doctor has been booked!", "Information", Messagebox.OK, Messagebox.INFORMATION);
            Executions.sendRedirect("/adminStaff/ViewAppointments.do?patientId=" + patient.getPatientId());
            setInfo();
        } catch (InterruptedException ex) {
            System.out.println(ex.getMessage());
        }
    }


}
